﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.APP.Classes;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_TimeAttMonthly : System.Web.UI.UserControl
    {
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private ITimeAttendantsBLL timeBLL = new TimeAttendantsBLL();
        private SsMng ssMng = new SsMng();
        private const int ItemsPerRequest = 10;
        private object _dataItem = null;
        private Main main = new Main();
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        //Initializing the new variable updating ViewState data.
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected override void OnLoad(System.EventArgs e)
        {
            try
            {
                txtActuaWorkingDay.Attributes.Add("onkeypress", "return isNumberKey(event)");
                txtDeliverDay.Attributes.Add("onkeypress", "return isNumberKey(event)");
                txtEducationDay.Attributes.Add("onkeypress", "return isNumberKey(event)");
                txtLateDay.Attributes.Add("onkeypress", "return isNumberKey(event)");
                txtOrdainDay.Attributes.Add("onkeypress", "return isNumberKey(event)");
                txtPlanWorkingDay.Attributes.Add("onkeypress", "return isNumberKey(event)");
                txtPrivateDay.Attributes.Add("onkeypress", "return isNumberKey(event)");
                txtQuotaVacationDay.Attributes.Add("onkeypress", "return isNumberKey(event)");
                txtSick.Attributes.Add("onkeypress", "return isNumberKey(event)");
                txtSickDay.Attributes.Add("onkeypress", "return isNumberKey(event)");
                txtVacationDay.Attributes.Add("onkeypress", "return isNumberKey(event)");

                if (DataItem != null)
                {
                    if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                    {
                        lblSeq.Text = DataBinder.Eval(DataItem, "time_att_month_seq").ToString();
                        cmbEmployee.Items.Add(new RadComboBoxItem(DataBinder.Eval(DataItem, "EMP_NAME").ToString(), DataBinder.Eval(DataItem, "EMP_SEQ").ToString()));
                        cmbEmployee.SelectedValue = DataBinder.Eval(DataItem, "emp_seq").ToString();

                        txtPlanWorkingDay.Text = DataBinder.Eval(DataItem, "plan_working_day_count").ToString();
                        txtActuaWorkingDay.Text = DataBinder.Eval(DataItem, "actual_working_day_count").ToString();
                        txtLateDay.Text = DataBinder.Eval(DataItem, "late_day_count").ToString();
                        txtSickDay.Text = DataBinder.Eval(DataItem, "sick_day_count").ToString();
                        txtPrivateDay.Text = DataBinder.Eval(DataItem, "private_day_count").ToString();
                        txtVacationDay.Text = DataBinder.Eval(DataItem, "vacation_day_count").ToString();
                        txtSick.Text = DataBinder.Eval(DataItem, "sick_count").ToString();
                        txtQuotaVacationDay.Text = DataBinder.Eval(DataItem, "quota_vacation").ToString();
                        txtDeliverDay.Text = DataBinder.Eval(DataItem, "deliver_day_count").ToString();
                        txtEducationDay.Text = DataBinder.Eval(DataItem, "edu_day_count").ToString();
                        txtOrdainDay.Text = DataBinder.Eval(DataItem, "ordain_day_count").ToString();
                    }
                    else
                    {

                    }
                    btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                    btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
                }
            }
            catch (Exception ex)
            { 
            
            }
        }
        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }
        protected void cmbEmployee_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            cmbEmployee.Items.Clear();
            Nullable<int> nJobSite = main.ConvertNullInt(Session["JOBSITESEQ"]);
            string empType = Convert.ToString(Session["EMPTYPE"]);

            DataSet ds = empBLL.LoadEmployees(null, "" + empType, null, nJobSite, true, e.Text);
            DataTable dt = ds.Tables[0];

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, dt.Rows.Count);
            e.EndOfItems = (endOffset == dt.Rows.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbEmployee.Items.Add(new RadComboBoxItem(dt.Rows[i]["FULL_NAME"].ToString(), dt.Rows[i]["EMP_SEQ"].ToString()));

                TableRow tr = new TableRow();
                TableCell tdPosRef = new TableCell();
                Label lblPosRef = new Label();
                lblPosRef.Text = dt.Rows[i]["EMP_SEQ"] + ":" + dt.Rows[i]["FULL_NAME"].ToString();
                tdPosRef.Width = Unit.Pixel(150);
                tdPosRef.Controls.Add(lblPosRef);
                tr.Cells.Add(tdPosRef);

                TableCell tdPosName = new TableCell();
                Label lblPosName = new Label();
                lblPosName.Text = dt.Rows[i]["POS_NAME"].ToString();
                tdPosName.Width = Unit.Pixel(100);
                tdPosName.Controls.Add(lblPosName);
                tr.Cells.Add(tdPosName);

                TableCell tdClassSpecName = new TableCell();
                Label lblClassSpecName = new Label();
                lblClassSpecName.Text = dt.Rows[i]["CLASS_NAME"].ToString();
                tdClassSpecName.Width = Unit.Pixel(100);
                tdClassSpecName.Controls.Add(lblClassSpecName);
                tr.Cells.Add(tdClassSpecName);

                TableCell tdJobName = new TableCell();
                Label lblJobName = new Label();
                lblJobName.Text = dt.Rows[i]["JOB_SITE_NAME"].ToString();
                tdJobName.Width = Unit.Pixel(150);
                tdJobName.Controls.Add(lblJobName);
                tr.Cells.Add(tdJobName);

                ((Table)cmbEmployee.Items[j].FindControl("tableEmployee")).Controls.Add(tr);
                j++;
            }
            e.Message = GetStatusMessage(endOffset, dt.Rows.Count);
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            int chkError = timeBLL.InsertTimeAttMonthly(cmbEmployee.SelectedValue
                , Int32.Parse(Session["MONTH"].ToString())
                , txtPlanWorkingDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtPlanWorkingDay.Text) : null
                , txtActuaWorkingDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtActuaWorkingDay.Text) : null
                , null
                , null
                , txtLateDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtLateDay.Text) : null
                , txtSickDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtSickDay.Text) : null
                , txtPrivateDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtPrivateDay.Text) : null
                , txtVacationDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtVacationDay.Text) : null
                , Int32.Parse(Session["YEAR"].ToString())
                , txtSick.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtSick.Text) : null
                , txtQuotaVacationDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtQuotaVacationDay.Text) : null
                , txtDeliverDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtDeliverDay.Text) : null
                , txtEducationDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtEducationDay.Text) : null
                , txtOrdainDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtOrdainDay.Text) : null
                , null
                , ssMng.EmpSeqI
                , new DateTime(DateTime.Now.Year > 2550 ? DateTime.Now.Year - 543:DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day));
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            decimal? Vac = txtVacationDay.Text.Length > 0 ? Convert.ToDecimal(txtVacationDay.Text) : 0;
            Vac = Vac > 0 ? Math.Ceiling(Vac.Value) : 0;
            int? Vac_int = Convert.ToInt32(Vac);
            decimal? Sic = txtSick.Text.Length > 0 ? Convert.ToDecimal(txtSick.Text) : 0;
            Sic = Sic > 0 ? Math.Ceiling(Sic.Value) : 0;
            int? Sic_int = Convert.ToInt32(Sic);
            decimal? Pri = txtPrivateDay.Text.Length > 0 ? Convert.ToDecimal(txtPrivateDay.Text) : 0;
            Pri = Pri > 0 ? Math.Ceiling(Pri.Value) : 0;
            int? Pri_int = Convert.ToInt32(Pri);
            //int? s = Convert.ToInt32(d);
            int chkError = timeBLL.UpdateTimeAttMonthly(cmbEmployee.SelectedValue.ToString()
                , Int32.Parse(Session["MONTH"].ToString())
                , txtPlanWorkingDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtPlanWorkingDay.Text) : null
                , txtActuaWorkingDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtActuaWorkingDay.Text) : null
                , null
                , null
                , txtLateDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtLateDay.Text) : null
                , txtSickDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(Sic_int) : null
                , Pri_int
                , Vac_int
                , Int32.Parse(Session["YEAR"].ToString())
                , txtSick.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtSick.Text) : null
                , txtQuotaVacationDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtQuotaVacationDay.Text) : null
                , txtDeliverDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtDeliverDay.Text) : null
                , txtEducationDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtEducationDay.Text) : null
                , txtOrdainDay.Text.Length > 0 ? (Nullable<int>)Convert.ToInt32(txtOrdainDay.Text) : null
                , null
                , ssMng.EmpSeqI
                , new DateTime(DateTime.Now.Year > 2550 ? DateTime.Now.Year - 543:DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day)
                );
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}